//
//  THJTradeContractVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2018/8/20.
//  Copyright © 2018年 河南德艺. All rights reserved.
//

#import "THJTradeContractVC.h"
#import <SDAutoLayout.h>
#import "THJContractDetailVC2.h"

@interface THJTradeContractVC ()
{
    UILabel * tempLab;
    NSMutableArray * contentArr;
}
@end

@implementation THJTradeContractVC

- (void)viewDidLoad {
    [super viewDidLoad];
    contentArr = [[NSMutableArray alloc] init];
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"填写合同信息"];
    }else{
        [self setNavBarWithTitle:@"填写合同信息"];
    }
    [self setUI];
    [self getData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void)setUI{
//    UIScrollView * scroV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, NavbarHeight, ScreenWidth, ScreenHeight)];
//    [self.view addSubview:scroV];
    NSArray * titleArr = @[@"出卖人",@"地  址",@"法  人",@"代理人",@"电  话",@"开户行",@"账  号",@"税  号"];
    for(NSInteger i = 0; i<8; i++){
        UILabel * label = [[UILabel alloc] init];
        label.text = titleArr[i];
        [self.view addSubview:label];
        
        UITextField * tf = [[UITextField alloc] init];
        tf.layer.borderWidth = 0.5;
        tf.tag = 100+i;
        [tf setBorderStyle: UITextBorderStyleRoundedRect];
        tf.layer.borderColor = SingleColor(orangeColor).CGColor;
        tf.layer.masksToBounds = YES;
        tf.layer.cornerRadius = 5;
        [self.view addSubview:tf];
        
        if(i == 0){
            label.sd_layout
            .topSpaceToView(self.view, NavbarHeight+20)
            .leftSpaceToView(self.view, 10)
            .widthIs(60)
            .heightIs(30);
            
            tf.sd_layout
            .leftSpaceToView(label, 10)
            .rightSpaceToView(self.view, 10)
            .centerYEqualToView(label)
            .heightIs(40);
            
        }else{
            label.sd_layout
            .topSpaceToView(tempLab, 20)
            .leftSpaceToView(self.view, 10)
            .widthIs(60)
            .heightIs(30);
            if(i == 4 || i == 6 || i == 7){
                tf.keyboardType  = UIKeyboardTypeNumberPad;
            }
            tf.sd_layout
            .leftSpaceToView(label, 10)
            .rightSpaceToView(self.view, 10)
            .centerYEqualToView(label)
            .heightIs(40);
        }
        tempLab = label;
    }
  
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(sureBtnAction) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = [UIFont systemFontOfSize:19];
    [btn setTitle:@"确认合同" forState:UIControlStateNormal];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 8;
    btn.backgroundColor = THJOrgColor;
    [btn setTitleColor:SingleColor(whiteColor) forState:UIControlStateNormal];
    
    btn.sd_layout
    .leftSpaceToView(self.view, 10)
    .rightSpaceToView(self.view, 10)
    .topSpaceToView(tempLab, 40)
    .heightIs(45);
    
    UIButton * btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:btn2];
    [btn2 setTitleColor:THJOrgColor forState:UIControlStateNormal];
    [btn2 setTitle:@"合同详情" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(contractDetail) forControlEvents:UIControlEventTouchUpInside];

    btn2.sd_layout
    .centerXEqualToView(btn)
    .topSpaceToView(btn, 10)
    .widthIs(100)
    .heightIs(30);
    
}
-(void)sureBtnAction{
//    BOOL isSuccess = YES;
//    NSMutableArray * contentArr = [[NSMutableArray alloc] init];
//    for (NSInteger i = 100; i<108; i++) {
//        UITextField * tf = [self.view viewWithTag:i];
//        NSString * text = tf.text;
//        [contentArr addObject:text];
//        NSLog(@"%@",text);
//        if([text isEqual:@""]){
//            isSuccess = NO;
//        }else{
//            isSuccess = YES;
//        }
//    }
//    if(!isSuccess){
//        [self HUDShowMsg:@"请完善信息" AfterDelayDimiss:1];
//        return;
//    }
//}
    BOOL isSuccess = YES;
    
    for (NSInteger i = 100; i<108; i++) {
        UITextField * tf = [self.view viewWithTag:i];
        NSString * text = tf.text;
        [contentArr addObject:text];
        NSLog(@"%@",text);
        if([text isEqual:@""]){
            isSuccess = NO;
        }else{
            isSuccess = YES;
        }
    }
    
    if(!isSuccess){
        [self HUDShowMsg:@"请完善信息" AfterDelayDimiss:1];
        return;
    }
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定合同内容无误并且订购吗" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了按钮1，进入按钮1的事件");
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定");
        [self postInfo];
    }];
    [actionSheet addAction:action1];
    [actionSheet addAction:action2];
    
    
    [self presentViewController:actionSheet animated:YES completion:nil];
}
-(void)postInfo{
    [self SVHUDShow];
    
    NSDictionary * dic = @{
                           @"type":@"ios",
                           @"itemid":[RSA encryptString:self.orderId publicKey:RSA_PubKey],
                           @"truename":[RSA encryptString:contentArr[0] publicKey:RSA_PubKey],
                           @"address":[RSA encryptString:contentArr[1] publicKey:RSA_PubKey],
                           @"faren":[RSA encryptString:contentArr[2] publicKey:RSA_PubKey],
                           @"dailiren":[RSA encryptString:contentArr[3] publicKey:RSA_PubKey],
                           @"tel":[RSA encryptString:contentArr[4] publicKey:RSA_PubKey],
                           @"accno":[RSA encryptString:contentArr[6] publicKey:RSA_PubKey],
                           @"yinhang":[RSA encryptString:contentArr[5] publicKey:RSA_PubKey],
                           @"shuihao":[RSA encryptString:contentArr[7] publicKey:RSA_PubKey]
                           };
    NSLog(@"提交的contentArr-%@",contentArr);
    [THJNetWork postNetDataFromUrl:@"http://p.thjb.net/thjblink/appdata.php?action=hetong_seller" parameters:dic callback:^(NSDictionary *  receiveData, NSError *error) {
        NSLog(@"卖家合同信息--%@",receiveData);
        [self SVHUDHiden];
        if([receiveData[@"status"] isEqual:@"success"]){
            [self HUDShowMsg:@"下单成功!" AfterDelayDimiss:1];
            [self performSelector:@selector(popRoot) withObject:nil afterDelay:1];
        }else{
            [self HUDShowMsg:@"订购失败" AfterDelayDimiss:1];
        }
    }];
}
-(void)popRoot{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)getData{
    NSDictionary * dic = @{@"mobile":[RSA encryptString:[THJTools getMobile] publicKey:RSA_PubKey],@"type":@"ios"};
    NSLog(@"tijiaoLL-%@",dic);
    [THJNetWork postNetDataFromUrl:@"http://p.thjb.net/thjblink/appdata.php?action=tax_info" parameters:dic callback:^(NSDictionary *  receiveData, NSError *error) {
        NSLog(@"税务信息--%@",receiveData);
        [self SVHUDHiden];
        if([receiveData[@"status"] isEqual:@"success"]){
            NSArray * arr = @[@"truename",@"address",@"faren",@"dailiren",@"tel",@"yinhang",@"accno",@"shuihao"];
            for (NSInteger i = 0; i<arr.count; i++) {
                NSString * str = [RSA decryptString:receiveData[@"data"][arr[i]] publicKey:RSA_PubKey];
                NSLog(@"styr-%@",str);
                UITextField * tf = [self.view viewWithTag:100+i];
                tf.text  = str;
            }
        }else{
            //            [self HUDShowMsg:@"获取信息失败" AfterDelayDimiss:1];
        }
    }];
}
-(void)contractDetail{
    BOOL isSuccess = YES;
    [contentArr removeAllObjects];
    for (NSInteger i = 100; i<108; i++) {
        UITextField * tf = [self.view viewWithTag:i];
        NSString * text = tf.text;
        NSLog(@"%@",text);
        if([text isEqual:@""]){
            isSuccess = NO;
            break;
        }else{
            isSuccess = YES;
        }
        [contentArr addObject:text];
    }
    
    if(!isSuccess){
        [self HUDShowMsg:@"请完善信息" AfterDelayDimiss:1];
        return;
    }
    THJContractDetailVC2 * vc = [THJContractDetailVC2 new];
    vc.orderId = self.orderId;
    vc.infoArr = contentArr;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
